package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 9/11/2023 2:12 PM
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierDao supplierDao;

    @Override
    public Map<String, Object> queryPageSuppliers(Integer page, Integer rows, String supplierName) {
        //1.计算分页的数据
        Integer jumpCount = (page - 1) * rows;

        //2.查询分页的数据
        List<Supplier> suppliers = supplierDao.queryPageSuppliers(jumpCount,rows,supplierName);

        //3.查询分页前的total的总记录数
        Integer total = supplierDao.queryPageSuppliersCount(supplierName);

        //4.组装map数据并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",suppliers);
        return map;
    }

    @Override
    public void save(Supplier supplier) {
        supplierDao.save(supplier);
    }

    @Override
    public Integer update(Integer supplierId, Supplier supplier) {
        // TODO 这个地方待会看下supplier里面有没有id就知道了 估计大概率没有
        supplier.setSupplierId(supplierId);
        return supplierDao.update(supplier);

    }

    @Override
    public void deleteSupplierByIds(String ids) {
        //将ids拆分成集合
        String[] split = StringUtils.split(ids, ",");
        List<String> idList = Arrays.asList(split);
        Integer count = supplierDao.deleteSupplierByIds(idList);
    }
}
